<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\Category;
use Illuminate\Http\Request;
use App\Http\Requests\CheckCategory;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    /**
     * 分类列表
     */
    public function index(){
        $list = Category::getCates();
        return view("admin.category.index",compact('list'));
    }

    /**
     * 分类添加
     */
    public function create(){
        $cates = Category::getCates();
        return view("admin.category.create",compact('cates'));
    }
    /**
     * 分类添加保存
     */
    public function store(CheckCategory $request,Category $category){
        $category->name = $request['name'];
        $category->pid  = $request['pid'];
        $category->sort = $request['sort'];
        checkReturn($category->save(),"添加成功");
        return redirect(route('admin.category.index'));
    }

    /**
     * 分类编辑
     * @$cate 实际上就是传递的参数
     */
    public function edit(Category $cate){
        $cates = Category::getCates();
        return view("admin.category.edit",compact('cate','cates'));
    }

    /**
     * 分类编辑保存
     */
    public function update(CheckCategory $request,Category $cate){
        $cate->name = $request['name'];
        $cate->pid  = $request['pid'];
        $cate->sort = $request['sort'];
        checkReturn($cate->save(),"编辑成功");
        return redirect(route('admin.category.index'));
    }

    /**
     * 分类删除
     */
    public function destory(Category $cate){
        checkReturn($cate->delete(),"删除成功");
        return redirect(route('admin.category.index'));
    }
}
